/**
  ******************************************************************************
  * File Name          : esp8266_common.c
  * Description        : This file provides code for the configuration
  *                      of the ESP8266 module.
  ******************************************************************************
  * @attention
  * Created by Chen Lu in 20210602
  *
  ******************************************************************************
  */

#include "stdlib.h"
#include "string.h"
#include "main.h"
#include "esp8266_common.h"
#include "esp8266_wifi_apsta.h"

uint8_t datarxtx_flag=0;

void esp8266_wifi_construct(WifiConfigStructure *wificonfig)
{
	wificonfig->wifi_ssid=(uint8_t*)"ESP8266_Group11";
	wificonfig->wifi_encryption=WPA_WPA2_PSK;
	wificonfig->wifi_password=(uint8_t*)"15987532";
}

int esp8266_set_apsta_wifimode(void)
{
	uint8_t *cmd = (uint8_t*) malloc (BUF_MAX_LENGTH*(sizeof(uint8_t)));
	sprintf((char*)cmd,"%s=3",AT_CWMODE);
	esp8266_send_cmd(cmd,20);
	free(cmd);
	return ESP8266_OK;
}

int esp8266_config_wifi_ap(WifiConfigStructure *wificonfig)
{
	uint8_t *cmd = (uint8_t*) malloc (BUF_MAX_LENGTH*(sizeof(uint8_t)));
	sprintf((char*)cmd,"%s=\"%s\",\"%s\",%d,%d",AT_CWSAP,
		wificonfig->wifi_ssid,wificonfig->wifi_password,5,wificonfig->wifi_encryption);
	esp8266_send_cmd((uint8_t*)cmd,20);
	free(cmd);
	return ESP8266_OK;
}

int esp8266_establish_UDP(uint8_t* remote_IP,int remote_port,int local_port,uint8_t mode)
{
	uint8_t *cmd = (uint8_t*) malloc (BUF_MAX_LENGTH*(sizeof(uint8_t)));
	sprintf((char*)cmd,"%s=\"%s\",\"%s\",%d,%d,%d",AT_CIPSTART,"UDP",remote_IP,remote_port,local_port,mode);
	esp8266_send_cmd((uint8_t*)cmd,20);
	free(cmd);
	return ESP8266_OK;
}

int esp8266_enable_passthrough(void)
{
	uint8_t *cmd = (uint8_t*) malloc (BUF_MAX_LENGTH*(sizeof(uint8_t)));
	sprintf((char*)cmd,"%s=1",AT_CIPMODE);
	esp8266_send_cmd((uint8_t*)cmd,20);
	free(cmd);
	return ESP8266_OK;
}

int esp8266_send_data_state(void)
{
	uint8_t *cmd = (uint8_t*) malloc (BUF_MAX_LENGTH*(sizeof(uint8_t)));
	sprintf((char*)cmd,"%s",AT_CIPSEND);
	esp8266_send_cmd((uint8_t*)cmd,20);
	datarxtx_flag = 1;
	free(cmd);
	return ESP8266_OK;
}

int esp8266_apsta_init(void)
{
	esp8266_wifi_construct(&ESP8266_WIFI_AP);
	if (esp8266_set_apsta_wifimode()!=ESP8266_OK)
	{
		printf("%s\r\n","Set AP&STA mode error!");
		return ESP8266_ERROR;
	}
	if (esp8266_config_wifi_ap(&ESP8266_WIFI_AP)!=ESP8266_OK)
	{
		printf("%s\r\n","Set WIFI AP parameters error!");
		return ESP8266_ERROR;
	}
	return ESP8266_OK;
}

int esp8266_close_trans(void)
{
	uint8_t *cmd = (uint8_t*) malloc (BUF_MAX_LENGTH*(sizeof(uint8_t)));
	sprintf((char*)cmd,"%s",AT_CIPCLOSE);
	esp8266_send_cmd((uint8_t*)cmd,20);
	free(cmd);
	return ESP8266_OK;
}

int esp8266_enter_UDP_passthrough(uint8_t* remote_IP,int remote_port)
{
	if (esp8266_close_trans()!=ESP8266_OK)
	{
		printf("%s\r\n","Quit UDP transmission error!");
		return ESP8266_ERROR;
	}
	if (esp8266_establish_UDP(remote_IP,remote_port,8080,0)!=ESP8266_OK)
	{
		printf("%s\r\n","Establish UDP transmission error!");
		return ESP8266_ERROR;
	}
	if (esp8266_enable_passthrough()!=ESP8266_OK)
	{
		printf("%s\r\n","Enable pass-through mode error!");
		return ESP8266_ERROR;
	}
	if (esp8266_send_data_state()!=ESP8266_OK)
	{
		printf("%s\r\n","Enter send data mode error!");
		return ESP8266_ERROR;
	}
	return ESP8266_OK;
}

